package com.landi.carkey.ui;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SignUpCallback;
import com.landi.carkey.R;
import com.landi.carkey.core.BaseActivity;
import com.landi.carkey.util.DialogFactory;
import com.landi.carkey.util.StringUtil;
import com.landi.carkey.util.ToastUtil;

/**
 * @Description {注册页面}
 * @Creator wushan
 * @Date 2016/12/28 10:37
 */

public class RegistActivity extends BaseActivity {

    private EditText mUserNameEt;
    private EditText mPassWordEt;
    private EditText mEmailEt;
    private EditText mPhoneNumberEt;


    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            DialogFactory.dismissAlert(RegistActivity.this);
            switch (msg.what){
                case 0://注册成功
                    ToastUtil.toast("注册成功");
                    finish();
                    break;
                case 125:
                    ToastUtil.toast("电子邮箱地址无效，请更换邮箱");
                    break;
                case 202:
                    ToastUtil.toast("该用户名已被注册，请更换用户名");
                    break;
                case 203:
                    ToastUtil.toast("该邮箱被注册，请更换邮箱");
                    break;
                case 214:
                    ToastUtil.toast("该手机号已被注册，请更换手机号");
                    break;
                default:
                    ToastUtil.toast(msg.what+", 注册失败，请稍后再试");
                    break;
            }
        }
    };

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_regist, R.string.regist, MODE_HOME);
    }

    @Override
    protected void setUpView() {

        mUserNameEt = (EditText)findViewById(R.id.et_regist_username);
        mPassWordEt = (EditText)findViewById(R.id.et_regist_password);
        mPhoneNumberEt = (EditText)findViewById(R.id.et_regist_phone);
        mEmailEt = (EditText)findViewById(R.id.et_regist_email);

    }

    @Override
    protected void setUpData() {

    }

    public void onMyButtonClick(View view) {
        switch (view.getId()) {
            case R.id.btn_regist:
                String username = mUserNameEt.getText().toString().trim();
                String password = mPassWordEt.getText().toString().trim();
                String phone = mPhoneNumberEt.getText().toString().trim();
                String email = mEmailEt.getText().toString().trim();

                if (TextUtils.isEmpty(username)){
                    ToastUtil.toast("用户名不能为空");
                    return;
                }
                if (TextUtils.isEmpty(password)){
                    ToastUtil.toast("密码不能为空");
                    return;
                }
                if (TextUtils.isEmpty(phone)){
                    ToastUtil.toast("手机号不能为空");
                    return;
                }
                if (!StringUtil.isValidPhoneNo(phone)){
                    ToastUtil.toast("手机号不可用");
                    return;
                }
                if (TextUtils.isEmpty(email)){
                    ToastUtil.toast("邮箱不能为空");
                    return;
                }
                if (!StringUtil.isEmail(email)){
                    ToastUtil.toast("邮箱不合法");
                    return;
                }

                regist(username,password,phone,email);

            break;
        }
    }

    private void regist(String username,String password,String phone,String email){


        DialogFactory.showLoadingDialog(this, "", "");

        AVUser user = new AVUser();// 新建 AVUser 对象实例
        user.setUsername(username);// 设置用户名
        user.setPassword(password);// 设置密码
        user.setMobilePhoneNumber(phone);//设置手机号
        user.setEmail(email);// 设置邮箱
        user.signUpInBackground(new SignUpCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                   handler.sendEmptyMessage(0);
                } else {
                    // 失败的原因可能有多种，常见的是用户名已经存在。
                    handler.sendEmptyMessage(e.getCode());
                }
            }
        });
    }
}
